<!DOCTYPE HTML><html lang="en">
<HEAD>

<meta name="copyright" content="Copyright (c) IBM Corporation and others 2006. This page is made available under license. For full details see the LEGAL in the documentation book that contains this page." >

<meta charset="utf-8">
<TITLE>
Working with resources in other file systems
</TITLE>

<link rel="stylesheet" type="text/css" HREF="../book.css">
</HEAD>
<BODY>
<h3>Working With Resources in Other File Systems</h3>

<p>
Most of the API in the <b><a href="../reference/api/org/eclipse/core/resources/IResource.html">IResource</a></b>
hierarchy works the same way
regardless of what kind of file system the resources are stored in.  However,
there are some particular APIs that you need to avoid when working with
resources in other file systems, because they are only designed to work
with the local file system.
</p>
<p>
In particular, the <b><a href="../reference/api/org/eclipse/core/resources/IResource.html">IResource</a>.getLocation</b> method is specified to return
the local file system path of the resource.  If the resource is in some other file
system, then this method is not applicable and will return <code>null</code>.
It is better to use the method <b>getLocationURI</b> instead,
which works regardless of what kind of file system the resource is stored in.
</p>
<p>
Similarly, the method 
<b><a href="../reference/api/org/eclipse/core/resources/IProjectDescription.html">IProjectDescription</a> getLocation</b>
and <b>setLocation</b> methods should be avoided because they are
only effective in the local file system. The URI-based location methods should
be used instead.
</p>
<h4>Local Caching</h4>
<p>
Say you are working with resources that are not in the local file system, but you
really need a local file.  For example, you may be using a library that has a 
dependency on <code>java.io.File</code>). In this case you can use the 
<code>IFileStore.toLocalFile</code> method to obtain a local copy of the file. Note that
this will only cause one file or directory to be cached locally, rather than an entire
directory tree.  Here is an example of using local caching to open a zip file on
an IFileStore:
</p>
<pre>
   IFileStore store = ...;//some file store
   java.io.File file = store.toLocalFile(EFS.NONE, null);
   if (file == null) {
      //we are not a local file store, so we need to cache a local copy
      file = store.toLocalFile(EFS.CACHE, null);
   }
   java.util.zip.ZipFile zip = new java.util.ZipFile(file);
</pre>
<h4>Linking to Other File Systems</h4>
<p>
You can use linked resources to create projects that draw together resources
from multiple file systems.  Simply use the method 
<b><a href="../reference/api/org/eclipse/core/resources/IFile.html">IFile</a>.createLink(URI, int, IProgressMonitor)</b> or
<b><a href="../reference/api/org/eclipse/core/resources/IFolder.html">IFolder</a>.createLink(URI, int, IProgressMonitor)</b>
to create a resource in an existing project whose contents are stored in another location in
an arbitrary file system.  You can even create links below other linked resources to
create arbitrary resource trees drawing from many different file systems.  Here
is a simple example that creates a sibling linked file that shares the same file
system location as the source:
</p>
<pre>
   IFile source = ...;//some source file
   IFile link = source.getParent().getFile(new Path(source.getName() + ".link"));
   link.createLink(source.getLocationURI(), IResource.NONE, null);
</pre>

</BODY>
</HTML>
